package com.company;

public class classworkTread2 extends Thread{

    private boolean flag1;
    public long time1;
    public long time0;
    public int x=100;
    public  int y=100;
    public void run(){
        if (flag1){
            this.tortoise();
        }else{
            this.rabbit();
        }

    }
    public classworkTread2(boolean flag1) {
        this.flag1 = flag1;

    }

    public synchronized void tortoise() {

            long l = System.currentTimeMillis();

            while (x > 0) {

                x = x - 1;
                if (1000 % 100 == 0) {
                    try {
                        sleep(500);

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                if(time1>time0) {
                    y = 0;
                }
                System.out.println(getName() + x);
                }

        long l1 = System.currentTimeMillis();
        time0 = l1- l;
        System.out.println("乌龟耗时" + time0);
            }


    public synchronized void rabbit() {

            long l3 = System.currentTimeMillis();

            while (y >0) {

                y = y - 5;
                if (1000 % 20 == 0) {
                    try {
                        sleep(500);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                if(time0>time1) {
                    x = 0;
                }
                System.out.println(getName() + y);
            }

        long l4 = System.currentTimeMillis();
        time1 = l4 - l3;
        System.out.println("兔子耗时" + time1);
        }
    }

